/*------------------------------------------------------------------------------*\
**   Copyright 2008-2009 Jonhnny Weslley                                        **
**                                                                              **
**   Licensed under the Apache License, Version 2.0 (the "License");            **
**   you may not use this file except in compliance with the License.           **
**   You may obtain a copy of the License at                                    **
**                                                                              **
**       http://www.apache.org/licenses/LICENSE-2.0                             **
**                                                                              **
**   Unless required by applicable law or agreed to in writing, software        **
**   distributed under the License is distributed on an "AS IS" BASIS,          **
**   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   **
**   See the License for the specific language governing permissions and        **
**   limitations under the License.                                             **
\*------------------------------------------------------------------------------*/

package org.scalashell.scalash

import org.scalashell.terminal._
import java.io.{PrintWriter, PrintStream}
import java.util.regex.Pattern

// Here be dragons
class PrettyPrinter(out: PrintStream) extends PrintWriter(out) with Renderer {

  override def print(text: String) = {
    var text2Print = text
    if (text startsWith "<console>") {
      text2Print = text.replaceAll("\\<console\\>\\:[0-9]+\\:\\serror\\:", "@|bold,red ERROR|")
    }

    val numberTransform = {s: String => "@|cyan " + s + "|"}
    
    val booleant = TextTransform(null, "Boolean", {s => "@|magenta " + s + "|"})
    val bytet = TextTransform(booleant, "Byte", numberTransform)
    val shortt = TextTransform(bytet, "Short", numberTransform)
    val intt = TextTransform(shortt, "Int", numberTransform)
    val longt = TextTransform(intt, "Long", numberTransform)
    val floatt = TextTransform(longt, "Float", numberTransform)
    val doublet = TextTransform(floatt, "Double", numberTransform)
    val chart = TextTransform(doublet, "Char", {s => "@|red '|@|cyan " + s + "|@|red '|"})
    val strt = TextTransform(chart, "java.lang.String", {s => "@|red \"|@|cyan " + s + "|@|red \"|"})
    val symbol = TextTransform(strt, "Symbol", {s => "@|bold,yellow " + s + "|"})

    text2Print = symbol.transform(text)

    super.print(render(text2Print))
  }

}

case class TextTransform(next: TextTransform, typeName: String, transformer: String => String) {

  val name = "([a-z0-9]+)"
  val first = name + "\\:\\s" + typeName.replace(".", "\\.")
  val patternString = first + "\\s\\=\\s(.*)\n"
  val pattern = Pattern compile patternString

  def transform(text: String): String = {
    val matcher = pattern.matcher(text)
    if (matcher.matches) {
      text.
        replaceAll(first, matcher.group(1)).
        replaceAll("=\\s(.*)$", "= " + transformer(matcher.group(2)))

    } else if (next ne null) {
      next.transform(text)

    } else {
      text
    }
  }

}
